﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Runtime.TagHelpers;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace WebApplication1.TagHelpers
{
    //// You may need to install the Microsoft.AspNetCore.Razor.Runtime package into your project
    //[HtmlTargetElement(Attributes = "spandescription")]
    //public class SpanDescriptionTagHelper : TagHelper
    //{
    //    //取属性值,方式3
    //    public string SpanDescription { get; set; }

    //    public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
    //    {
    //        //拿到标记属性为spandescription的标签里的内容
    //        var content = await output.GetChildContentAsync();

    //        var txtcontent = content.GetContent();


    //        //取属性值方式1
    //        //string spanDescriptionValue="";

    //        //if (output.Attributes.TryGetAttribute("spandescription", out TagHelperAttribute attr))
    //        //{
    //        //    spanDescriptionValue = attr.Value.ToString();
    //        //}


    //        //取属性值方式2
    //        var spanDescriptionValue = output.Attributes["spandescription"].Value.ToString();

    //        output.Attributes.RemoveAll("spandescription");//去除标记中的,spandescription属性

    //        output.PreElement.SetHtmlContent($"<p class=\"{spanDescriptionValue}\">");

    //        output.PostElement.SetHtmlContent("</p>");

    //        output.Content.SetContent(txtcontent);


    //    }
    //}


    // You may need to install the Microsoft.AspNetCore.Razor.Runtime package into your project
    [HtmlTargetElement(Attributes = "span-description")]
    public class SpanDescriptionTagHelper : TagHelper
    {
        //取属性值,方式3
        public string SpanDescription { get; set; }

        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            //拿到标记属性为spandescription的标签里的内容
            var content = await output.GetChildContentAsync();

            var txtcontent = content.GetContent();

            output.Attributes.RemoveAll("spandescription");//去除标记中的,spandescription属性

            output.PreElement.SetHtmlContent($"<p class=\"{SpanDescription}\">");

            output.PostElement.SetHtmlContent("</p>");

            output.Content.SetContent(txtcontent);


        }
    }


}
